package com.businessportal.web.dataaccess.dao;

import com.businessportal.web.dataaccess.entities.Address;
import com.businessportal.web.dataaccess.utils.HibernateUtil;
import org.hibernate.Query;
import org.hibernate.Session;

import java.util.List;

/**
 * Created by Vadim on 1/15/2015.
 */
public class AddressDao implements IAddressDao {
    @Override
    public Address getById(Long aLong) {
        Session session = null;
        Address retValue = null;
        try{
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();

            retValue = (Address)session.get(Address.class, aLong);

            session.getTransaction().commit();
        } catch (Exception e){

            retValue = null;
            session.getTransaction().rollback();

            e.printStackTrace();
        } finally {
            if(null != session){
                session.close();
            }
        }

        return retValue;
    }

    @Override
    public List<Address> getAll() {
        Session session = null;
        List<Address> retValue = null;

        try{
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();

            Query userQuery = session.createQuery("From Address where 1=1");

            retValue = userQuery.list();

            session.getTransaction().commit();

        } catch (Exception e){

            session.getTransaction().rollback();

            e.printStackTrace();
        } finally {
            if(null != session){
                session.close();
            }
        }

        return retValue;
    }

    @Override
    public void save(Address address) {
        Session session = null;
        try{
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();

            session.save(address);

            session.getTransaction().commit();
        } catch (Exception e){

            session.getTransaction().rollback();

            e.printStackTrace();
        } finally {
            if(null != session){
                session.close();
            }
        }
    }

    @Override
    public void update(Address address) {
        Session session = null;
        try{
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();

            session.update(address);

            session.getTransaction().commit();
        } catch (Exception e){

            session.getTransaction().rollback();

            e.printStackTrace();
        } finally {
            if(null != session){
                session.close();
            }
        }
    }

    @Override
    public void delete(Address address) {
        Session session = null;
        try{
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();

            session.delete(address);

            session.getTransaction().commit();
        } catch (Exception e){

            session.getTransaction().rollback();

            e.printStackTrace();
        } finally {
            if(null != session){
                session.close();
            }
        }
    }

    @Override
    public void deleteById(Long aLong) {
        Session session = null;
        try{
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();

            session.delete(new Address(aLong));

            session.getTransaction().commit();
        } catch (Exception e){

            session.getTransaction().rollback();

            e.printStackTrace();
        } finally {
            if(null != session){
                session.close();
            }
        }
    }
}
